package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.MessageDAO;
import beans.CommentBean;
import beans.EventBean;
import beans.MessageBean;
import beans.UserBean;

/**
 * Servlet implementation class WriteCommentServlet
 */
@WebServlet("/WriteCommentServlet")
public class WriteCommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WriteCommentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		UserBean writer = (UserBean)session.getAttribute("currentSessionUser");
		EventBean event = (EventBean)session.getAttribute("currentEvent");
		String body = request.getParameter("commentbody");
		CommentBean comment = new CommentBean();
		comment.setWriter(writer.getEmail());
		comment.setEventHost(event.getHost());
		comment.setEventStamp(event.getStartTimeStamp());
		comment.setBody(body);
		String start = event.getStartTimeStamp().toString();
		start = start.substring(0, 19);
		start = start.replace("-", "");
		start = start.replace(":", "");
		start = start.replace(" ", "");
		MessageDAO.writeComment(comment);
		response.sendRedirect("EventServlet?host=" + event.getHost() + "&start=" + start);
	}	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
